package com.itwn.controller;

import com.github.pagehelper.PageInfo;
import com.itwn.entity.Schedule;
import com.itwn.entity.User;
import com.itwn.result.ResponseResult;
import com.itwn.service.CommentService;
import com.itwn.service.UserService;
import com.itwn.vo.CommentVo;
import com.itwn.vo.ScheduleVo;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.xml.stream.events.Comment;

@RestController
@RequestMapping("user")
public class UserController {
    private UserService userService;


    @Autowired
    public UserController(UserService userService) {
        this.userService = userService;

    }

    @GetMapping("/list")
    public ResponseResult<PageInfo<User>> showUserList(@RequestParam(value = "pageNum",defaultValue = "1") Integer pageNum,
                                                 @RequestParam(value = "pageSize",defaultValue = "5")Integer pageSize,
                                                 @RequestParam(value = "userName",required = false)String userName){

        ResponseResult<PageInfo<User>> result = new ResponseResult<>();
        try{
            PageInfo<User> pageInfo = userService.showUserList(pageNum,pageSize,userName);
            result.setCode(200);//查询成功
            result.setMsg("查询成功");
            result.setData(pageInfo);

        }catch (Exception e){
            result.setCode(500);//查询失败
            result.setMsg("查询失败");
        }
        return result;
    }

    @PostMapping("/delete/{userId}")
    public ResponseResult<User> deleteUser(@PathVariable("userId") Integer userId){
        ResponseResult<User> result = new ResponseResult<>();
        try{
            userService.deleteUser(userId);
            result.setCode(200);
            result.setMsg("删除成功");
        }catch (Exception e){
            result.setCode(500);
            result.setMsg("删除失败");
        }

        return result;


    }
}
